/*
 * stm32f4xx_it.c
 *
 *  Created on: 30.11.2012
 *      Author: Michael
 */

#include "stm32f4xx.h"
#include "hardware.h"
#include "yprintf.h"
#include "Threads/global.h"

extern Systemstate state;
extern Camera cam;

int cnt = 0;
int start = 1;
int count = 0;

extern "C" void EXTI0_IRQHandler(void) {
	xprintf("Take Snapshot\n");
	cam.Capture();
	/* Clear the EXTI line pending bit */
	EXTI_ClearITPendingBit(EXTI_Line0 );
}

extern "C" void DMA1_Stream6_IRQHandler(void){
//	xprintf("Picture IT!\n");
	if(DMA_GetITStatus(DMA1_Stream6, DMA_IT_TCIF6) == SET){
		xprintf("Frame send! -> Continue with Payload\n");
		state.frameReceived = false;
		DMA_ClearFlag(DMA1_Stream6, DMA_IT_TCIF6);
	}
}

extern "C" void DCMI_IRQHandler(void) {
	if(DCMI_GetITStatus(DCMI_IT_VSYNC) == SET)
	{
		if(start == 0)
		{
			start = 1;
		}
		else
		{
			start = 0;
		}
//		xprintf("VSYNC \n\n");
		DCMI_ClearFlag(DCMI_IT_VSYNC);
	}
	else if(DCMI_GetITStatus(DCMI_IT_LINE) == SET)
	{
		if(start == 1)
		{
			count++;
		}
		else
		{
			if(count != 0)
			{
//				xprintf("count: %d \n\n", count); //just for counting the number of line
			}
			count = 0;
		}
		DCMI_ClearFlag(DCMI_IT_LINE);
	}
	else if(DCMI_GetITStatus(DCMI_IT_FRAME) == SET)
	{
//		xprintf("DCMI_FLAG_FRAMERI \n\n");
		state.frameReceived = true;
		DCMI_ClearFlag(DCMI_IT_FRAME);
	}
	else if(DCMI_GetITStatus(DCMI_IT_ERR)== SET)
	{
//		xprintf("DCMI_FLAG_ERRRI \n\n");
		DCMI_ClearFlag(DCMI_IT_ERR);
	}
	else if(DCMI_GetITStatus(DCMI_IT_OVF) == SET)
	{
//		xprintf("DCMI_FLAG_OVFRI \n\n");
		DCMI_ClearFlag(DCMI_IT_OVF);
	}
}
